package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.*;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.XiaoShouService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: LuXi
 * @Date: 2021/3/3 16:36
 * @Description:
 */
@Service
public class XiaoShouServiceImpl implements XiaoShouService {
    @Resource
    private SesUserDao sesUserDao;
    @Resource
    private SesUserOrderDao sesUserOrderDao;
    @Resource
    private SesBedMessageDao sesBedMessageDao;
    @Resource
    private SesHospitalinfoDao sesHospitalinfoDao;
    @Resource
    private SesHospitalinfoDepartmentDao sesHospitalinfoDepartmentDao;

    /**
     * 获取所有装维销售情况
     *
     * @return
     */
    @Override
    public Map<String, Object> getXiaoShou(XiaoShouSearch xiaoShouSearch, Integer pageNum) {
        Map<String, Object> map = new HashMap<>();

        //获取所有股东分销商
        SesUserExample userExample = new SesUserExample();
        SesUserExample.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andUsertypeIn(Arrays.asList(4, 10, 11));
        //查询股东
        if (xiaoShouSearch.getUser() != null && xiaoShouSearch.getUser() != 0) {
            userCriteria.andUseridEqualTo(xiaoShouSearch.getUser());
        }

        List<SesUser> users = sesUserDao.selectByExample(userExample);
        map.put("users", users);
        List<Long> userIds = new ArrayList<>();
        for (SesUser su : users) {
            userIds.add(su.getUserid());
        }
        //获取股东对应所有订单
        SesUserOrderExample orderExample = new SesUserOrderExample();
        SesUserOrderExample.Criteria orderCriteria = orderExample.createCriteria();

        //查询医院与科室
        SesBedMessageExample bedMessageExample = new SesBedMessageExample();
        SesBedMessageExample.Criteria bedMessageExampleCriteria = bedMessageExample.createCriteria();
        if (xiaoShouSearch.getHospital() != null && xiaoShouSearch.getHospital() != 0) {
            bedMessageExampleCriteria.andHospitalinfoidEqualTo(xiaoShouSearch.getHospital());
        }
        if (xiaoShouSearch.getDepartment() != null && xiaoShouSearch.getDepartment() != 0) {
            bedMessageExampleCriteria.andHospitalinfoDepartmentidEqualTo(xiaoShouSearch.getDepartment());
        }
        List<SesBedMessage> bedSearch = sesBedMessageDao.selectByExample(bedMessageExample);
        List<Long> bed_search_id = new ArrayList<>();
        for (SesBedMessage sb : bedSearch) {
            bed_search_id.add(sb.getBedmessageid());
        }
        orderCriteria.andUserUseridIn(userIds);
        orderCriteria.andBedMessageidIn(bed_search_id);

        PageHelper.startPage(pageNum, 5);
        List<SesUserOrder> orders = sesUserOrderDao.selectByExample(orderExample);
        for (SesUserOrder suo : orders) {
            Long userUserid = suo.getUserUserid();
            suo.setUsername(sesUserDao.selectByPrimaryKey(userUserid).getUsername());

            //获取该订单所属床位
            Long bedMessageid = suo.getBedMessageid();
            SesBedMessage bed = sesBedMessageDao.selectByPrimaryKey(bedMessageid);
            //获得医院与科室
            suo.setHospital(sesHospitalinfoDao.selectByPrimaryKey(bed.getHospitalinfoid()).getHospitalinfoname());
            suo.setDepartment(sesHospitalinfoDepartmentDao.selectByPrimaryKey(bed.getHospitalinfoDepartmentid()).getDepartmentname());

            //获得该医院与科室的床位数
            SesBedMessageExample bedExample = new SesBedMessageExample();
            SesBedMessageExample.Criteria bedCriteria = bedExample.createCriteria();
            bedCriteria.andHospitalinfoidEqualTo(bed.getHospitalinfoid());
            bedCriteria.andHospitalinfoDepartmentidEqualTo(bed.getHospitalinfoDepartmentid());
            List<SesBedMessage> hd_beds = sesBedMessageDao.selectByExample(bedExample);
            suo.setCwCount(hd_beds.size());
            //获得拥有该床位的订单数量
            List<Long> bedId = new ArrayList<>();
            for (SesBedMessage sbm : hd_beds) {
                bedId.add(sbm.getBedmessageid());
            }
            SesUserOrderExample order_example = new SesUserOrderExample();
            SesUserOrderExample.Criteria orderExampleCriteria = order_example.createCriteria();
            orderExampleCriteria.andBedMessageidIn(bedId);
            orderExampleCriteria.andUserorderstatusIn(Arrays.asList(2, 4));
            List<SesUserOrder> orderList = sesUserOrderDao.selectByExample(order_example);
            suo.setZlCount(orderList.size());
            Double sum = 0D;
            for (SesUserOrder s : orderList) {
                sum += s.getUserorderrent();
            }
            suo.setMoney(sum);
        }
        PageInfo<SesUserOrder> info = new PageInfo<>(orders);
        map.put("orders", info.getList());
        map.put("pageInfo", info);
        map.put("hospital", sesHospitalinfoDao.selectByExample(null));
        map.put("department", sesHospitalinfoDepartmentDao.selectByExample(null));
        return map;
    }

    /**
     * 获得会计报表
     * @param pageNum
     * @return
     */
    @Override
    public Map<String, Object> getKuaiji(Integer pageNum) {
        Map<String, Object> map = new HashMap<>();
        //每条会计详情
        List<SesUserOrder> orders = sesUserOrderDao.selectByExample(null);
        Integer cwTotal =0;
        Integer zlTotal =0;
        Double moTotal =0D;
        for (SesUserOrder temp:orders){
            String old = temp.getUserorderstarttime();
            SesUserOrderExample orderExample = new SesUserOrderExample();
            SesUserOrderExample.Criteria orderExampleCriteria = orderExample.createCriteria();
            orderExampleCriteria.andUserorderstarttimeEqualTo(old);
            List<SesUserOrder> table_order = sesUserOrderDao.selectByExample(orderExample);
            temp.setZlCount(table_order.size());//租赁
            Set<Long> set = new HashSet<>();
            Double moneyCount = 0D;
            for (SesUserOrder order_bed:table_order) {
                moneyCount += order_bed.getUserorderrent();
                set.add(order_bed.getBedMessageid());
            }
            temp.setCwCount(set.size());
            temp.setMoney(moneyCount);
            cwTotal += temp.getCwCount();
            zlTotal += temp.getZlCount();
            moTotal += temp.getMoney();
        }
        //*************************************************//
        Set<String> startTime = new HashSet<>();
        for (SesUserOrder temp:orders){
            startTime.add(temp.getUserorderstarttime());
        }
        Iterator<String> iterator = startTime.iterator();
        List<String> time = new ArrayList<>();
        while (iterator.hasNext()){
            time.add(iterator.next());
        }
        SesUserOrderExample sesUserOrderExample = new SesUserOrderExample();
        SesUserOrderExample.Criteria orderCriteria = sesUserOrderExample.createCriteria();
        orderCriteria.andUserorderstarttimeIn(time);
        //分页
        PageHelper.startPage(pageNum, 5);
        List<SesUserOrder> orderList = sesUserOrderDao.selectByExample(sesUserOrderExample);
        for (SesUserOrder temp:orderList){
            String old = temp.getUserorderstarttime();
            SesUserOrderExample orderExample = new SesUserOrderExample();
            SesUserOrderExample.Criteria orderExampleCriteria = orderExample.createCriteria();
            orderExampleCriteria.andUserorderstarttimeEqualTo(old);
            List<SesUserOrder> table_order = sesUserOrderDao.selectByExample(orderExample);
            temp.setZlCount(table_order.size());//租赁
            Set<Long> set = new HashSet<>();
            Double moneyCount = 0D;
            for (SesUserOrder order_bed:table_order) {
                moneyCount += order_bed.getUserorderrent();
                set.add(order_bed.getBedMessageid());
            }
            temp.setCwCount(set.size());
            temp.setMoney(moneyCount);
        }
        PageInfo<SesUserOrder> info = new PageInfo<>(orderList);

        map.put("cwCount",cwTotal);
        map.put("zlCount",zlTotal);
        map.put("moneyCount",moTotal);

        map.put("orders", info.getList());
        map.put("pageInfo", info);
        return map;
    }
}
